package com.zzh.partnersys.im.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzh.partnersys.common.enums.SessionTypeEnum;
import com.zzh.partnersys.common.exception.BusinessException;
import com.zzh.partnersys.common.exception.ResultCodeEnum;
import com.zzh.partnersys.common.util.TokenUtils;
import com.zzh.partnersys.im.entity.ChatSession;
import com.zzh.partnersys.im.entity.request.ChatSessionCreateRequest;
import com.zzh.partnersys.im.mapper.ChatSessionMapper;
import com.zzh.partnersys.im.service.ChatSessionService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 单聊会话表 服务实现类
 * </p>
 *
 * @author zzh
 * @since 2025-11-19
 */
@Service
public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatSession> implements ChatSessionService {

    /**
     * 创建单聊会话
     * @param createRequest 创建单聊会话请求参数
     * @return ChatSession
     */
    @Override
    public ChatSession createChatSession(ChatSessionCreateRequest createRequest, HttpServletRequest request) {
        Long assistantId = createRequest.getAssistantId();
        //查看助手是否存在
        if (assistantId == null) {
            throw new BusinessException(ResultCodeEnum.ASSISTANT_NOT_FOUND);
        }
        //todo 远程调用服务，创建会话

        //区分会话类型
        if (SessionTypeEnum.USER_TO_AI.getCode() == createRequest.getSessionType()){
            return createUserToAIChatSession(createRequest, request);
        }
        return createUserToUserChatSession(createRequest, request);
    }

    private ChatSession createUserToUserChatSession(ChatSessionCreateRequest createRequest, HttpServletRequest request) {
        return null;
    }

    private ChatSession createUserToAIChatSession(ChatSessionCreateRequest createRequest, HttpServletRequest request) {
//        ChatSession chatSession = new ChatSession();
//        chatSession.setSessionType(createRequest.getSessionType());
//        chatSession.setAssistantId(assistantId);
//        chatSession.setUser1Id(TokenUtils.getCurrentUserIdOrThrow(request));
//        chatSession.setUser2Id(0L);
//        chatSession.setTitle();
//        chatSession.setLastMsgTime();
//        chatSession.setLastReadSequence();
//        chatSession.setIsDelete();
//        chatSession.setCreateTime();
//        chatSession.setUpdateTime();
        return null;
    }

}
